package api;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-11-03
 * Time: 14:18
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       HttpSession session=req.getSession(false);
       if(session==null){
           //当前状态未登录，不做处理，跳转到登录
           resp.sendRedirect("login.html");
           return;
       }
       //在登录成功之后，session中会有user这个属性，
        // 把他删除之后，状态就成了为登录
        session.removeAttribute("user");
       resp.sendRedirect("login.html");
    }
}
